package com.wd.live.viewmodel

import ApiService
import HttpUtils
import android.content.Intent
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.wd.live.model.base.BaseViewModel
import com.wd.live.model.bean.LocalData
import com.wd.live.view.activity.HomeActivity
import com.wd.live.wxapi.UserInfo
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 作者：马海钊
 * 时间：2023/8/16 15:48
 * 功能：微信登录
 */
class LoginViewModel : BaseViewModel() {
    public var getNavigationBean = MutableLiveData<LocalData.UserInfo?>()
    private val userInfoLiveData: MutableLiveData<LocalData.UserInfo> by lazy {
        MutableLiveData<LocalData.UserInfo>()
    }
    val userInfo: MutableLiveData<LocalData.UserInfo>
        get() = userInfoLiveData

    fun getWxCode(wxApi: IWXAPI) {
        //拉起微信登录
        val req = SendAuth.Req()
        //授权域 获取用户个人信息则填写snsapi_userinfo
        req.scope = "snsapi_userinfo"
        //state用于保持请求和回调的状态，授权请求后原样带回给第三方。该参数可用于防止csrf攻击（跨站请求伪造攻击）
        // 建议第三方带上该参数，可设置为简单的随机数加session进行校验
        req.state = "test_login"
        //调用api接口，发送数据到微信
        wxApi.sendReq(req)
    }

    fun doLogin(code: String) {
        val apiService = HttpUtils.api
        val params = HashMap<String, Any>()
        params["code"] = code
        apiService?.wxLogin(params)?.let {
            it.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ bean ->

                    userInfoLiveData.value = bean.result
                    Log.d("provideSystemApi", bean.toString())
                }, { error ->
                    Log.d("provideSystemApi", error.message.toString())
                })
        }

    }

    val liveData: LiveData<String>? = null
}
